Skip to content

[Fix] Android/ColorProps: ColorProps with value null should be defaultColor instead of transparent#29830

Closed
hank121314 wants to merge 12 commits into
react:mainfrom
hank121314:fix/DynamicColorText
Closed

[Fix] Android/ColorProps: ColorProps with value null should be defaultColor instead of transparent#29830
hank121314 wants to merge 12 commits into
react:mainfrom
hank121314:fix/DynamicColorText

Conversation

@hank121314

@hank121314 hank121314 commented Sep 2, 2020

Copy link
Copy Markdown
Contributor

Summary

This pr:

Because most of @ReactProps(name = ViewProps.COLOR) accept @ Nullable Integer.
For example:
https://github.com/facebook/react-native/blob/abb6433f506851430dffb66f0dd34c1e70a223fe/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L472-L479

After update to react-native 0.63.2 to make PlatformColor work, there is a new ColorPropSetter.
https://github.com/facebook/react-native/blob/abb6433f506851430dffb66f0dd34c1e70a223fe/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java#L194-L215

But ColorPropSetter won't return an nullable value with getValueOrDefault, it will always return it's defaultValue which is 0.
And 0 is equal to TRANSPARENT, will cause disappear.

Changelog

[Android] [Fixed] - ColorProps with value null should be defaultColor instead of transparent

Test Plan

Please initiated a new project and replaced the app with the following code:

import * as React from 'react';
import {Text, View, TouchableOpacity, PlatformColor} from 'react-native';

export default function App() {
  const [active, setActive] = React.useState(false);

  return (
    <View>
      <Text style={active ? {color: 'green'} : null}>Example</Text>
      <Text
        style={
          active ? {color: PlatformColor('@android:color/holo_purple')} : null
        }>
        Example2
      </Text>
      <TouchableOpacity onPress={() => setActive(!active)}>
        <Text>Toggle Active</Text>
      </TouchableOpacity>
    </View>
  );
}

Thanks you so much for your code review!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 2, 2020
@react-native-bot react-native-bot added Bug Platform: Android Android applications. labels Sep 2, 2020
@analysis-bot

analysis-bot commented Sep 2, 2020

Copy link
Copy Markdown
Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 9,239,782 -11
android hermes armeabi-v7a 8,749,720 -5
android hermes x86 9,702,093 -6
android hermes x86_64 9,667,310 -16
android jsc arm64-v8a 10,886,756 -47
android jsc armeabi-v7a 9,787,976 -54
android jsc x86 10,944,476 -53
android jsc x86_64 11,551,223 -47

Base commit: 4246c75

@analysis-bot

analysis-bot commented Sep 2, 2020

Copy link
Copy Markdown
Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 4246c75

@safaiyeh

safaiyeh commented Sep 4, 2020

Copy link
Copy Markdown
Contributor

Nice work! @hank121314 I built RNTester to test this out:
Screen Recording 2020-09-03 at 11 32 06 PM

Working as expected, tagging PR submitted on the issue.

@safaiyeh

safaiyeh commented Sep 4, 2020

Copy link
Copy Markdown
Contributor

cc @JoshuaGross

@hank121314

Copy link
Copy Markdown
Contributor Author

Nice work! @hank121314 I built RNTester to test this out:
Screen Recording 2020-09-03 at 11 32 06 PM

Working as expected, tagging PR submitted on the issue.

Thank you so much for help and testing! 😄

yskoht added a commit to newn-team/react-native that referenced this pull request Oct 6, 2020
@hank121314 hank121314 changed the title [Fix] Android/Text: Conditionally change <Text /> color cause text invisible [Fix] Android/ColorProps: ColorProps with null should be defaultColor instead of transparent Oct 13, 2020
@hank121314 hank121314 changed the title [Fix] Android/ColorProps: ColorProps with null should be defaultColor instead of transparent [Fix] Android/ColorProps: ColorProps with value null should be defaultColor instead of transparent Oct 13, 2020
@Danite

Danite commented Oct 20, 2020

Copy link
Copy Markdown

I think when the color is undefined it should also fallback to the defaultColor.

Take this case:
<Text style={[someTextStyleWithoutColor, isActive && activeTextStyle]}> Hello there! </Text>
it will make the text transparent once is not active.

@hank121314

Copy link
Copy Markdown
Contributor Author

I think when the color is undefined it should also fallback to the defaultColor.

Take this case:
<Text style={[someTextStyleWithoutColor, isActive && activeTextStyle]}> Hello there! </Text>
it will make the text transparent once is not active.

Yes, this pr is also included undefined in javascript.

@analysis-bot analysis-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code analysis results:

@Override
protected @Nullable Object getValueOrDefault(Object value, Context context) {
if (value != null) {
return ColorPropConverter.getColor(value, context);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

google-java-format suggested changes:

@@ -337 +337 @@
-          return ColorPropConverter.getColor(value, context);
+        return ColorPropConverter.getColor(value, context);

set0gut1 pushed a commit to newn-team/react-native that referenced this pull request Dec 11, 2020

@hank121314 hank121314 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply google-java-format suggested changes.

@Babar-Memon

Copy link
Copy Markdown

i have a same problem on image component anyone can help me out to resolve this issue

@hank121314

Copy link
Copy Markdown
Contributor Author

i have a same problem on image component anyone can help me out to resolve this issue

Hi @Babar-Memon , you can try to patch this pr with Building from source.
Or set color props to any colors except null.

@fabOnReact fabOnReact left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot @hank121314. Your pr also fixes #29378 (not linked in your pr summary), I was searching for the solution as explained in #29412 (comment) and then found your PR from another related issue.

I checkout out your branch and as in the below test case, verified that it fixes also #29378

CLICK TO OPEN TESTS RESULTS

BEFORE AFTER

Thanks a lot. 🙏 ☮️

@hank121314

Copy link
Copy Markdown
Contributor Author

Hi @fabriziobertoglio1987 .
Thanks for your investigation and review!
Hope this pr can be merged soon. 😄

@StanislavMayorov

Copy link
Copy Markdown

@fabriziobertoglio1987 @hank121314 Hi. Could you merge it please?

@taylorkline

Copy link
Copy Markdown

This did not fix ActivityIndicator from being transparent for me. #30057 worked for me.

@taylorkline

Copy link
Copy Markdown

Actually, in general, this fix is not working for me on 0.64.2.

@hank121314

Copy link
Copy Markdown
Contributor Author

Hi @taylorkline, did you try to patch this pr with Building from source?
If it still does not work, could you give me some reproducible code?
Thanks!

@janicduplessis janicduplessis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, sorry this is taking forever to merge, will ping some people at fb.

@taylorkline

Copy link
Copy Markdown

@hank121314 No, I did not - my apologies, likely user error on my part then.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@lunaleaps has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@lunaleaps lunaleaps self-assigned this Aug 10, 2021
@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Aug 18, 2021
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@lunaleaps merged this pull request in 842bcb9.

kelset pushed a commit that referenced this pull request Aug 19, 2021
…or instead of transparent (#29830)

Summary:
This pr:
- Fixes: #30183
- Fixes: #30056
- Fixes: #29950
- Fixes: #29717
- Fixes: #29495
- Fixes: #29412
- Fixes: #29378

Because most of ReactProps(name = ViewProps.COLOR) accept @ Nullable Integer.
For example:
https://github.com/facebook/react-native/blob/abb6433f506851430dffb66f0dd34c1e70a223fe/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L472-L479

After update to react-native 0.63.2 to make PlatformColor work, there is a new ColorPropSetter.
https://github.com/facebook/react-native/blob/abb6433f506851430dffb66f0dd34c1e70a223fe/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java#L194-L215

But ColorPropSetter won't return an nullable value with getValueOrDefault, it will always return it's defaultValue which is 0.
And 0 is equal to TRANSPARENT, will cause <Text /> disappear.
## Changelog

[Android] [Fixed] - ColorProps with value null should be defaultColor instead of transparent

Pull Request resolved: #29830

Test Plan:
Please initiated a new project and replaced the app with the following code:
```
import * as React from 'react';
import {Text, View, TouchableOpacity, PlatformColor} from 'react-native';

export default function App() {
  const [active, setActive] = React.useState(false);

  return (
    <View>
      <Text style={active ? {color: 'green'} : null}>Example</Text>
      <Text
        style={
          active ? {color: PlatformColor('android:color/holo_purple')} : null
        }>
        Example2
      </Text>
      <TouchableOpacity onPress={() => setActive(!active)}>
        <Text>Toggle Active</Text>
      </TouchableOpacity>
    </View>
  );
}
```

Thanks you so much for your code review!

Reviewed By: JoshuaGross

Differential Revision: D30209262

Pulled By: lunaleaps

fbshipit-source-id: bc223f84a92f742266cb7b40eb26722551940d76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Needs: React Native Team Attention Platform: Android Android applications.

Projects

None yet